import { getMd5Sign } from '../../utils/util'
const request = require('../config/index')

/**
 * 获取加盟配置
 */
export const getVehicleSystem = () => {
    const body = {
        API_Model: 'DriverPeople',
        API_Name: 'Get_System',
        Data: {}
    }
    const md5 = getMd5Sign(body)

    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5
            })
            .then((res: any) => {
                resolve(res.data.data)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

/**
 * 获取车辆类型
 * @param openid
 * @param token
 */
export const obtainVehicleType = () => {
    const body = {
        API_Model: 'Local',
        API_Name: 'get_car_type_list',
        Data: {}
    }
    const md5 = getMd5Sign(body)

    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5
            })
            .then((res: any) => {
                resolve(res.data.data)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

/**
 * 获取两点之间的距离和价格
 * @param startPoint
 * @param endPoint
 * @param carId
 * @param goTime
 */
export const getDistantAndMoney = (startPoint: string, endPoint: string, carId: string, goTime: string) => {
    const body = {
        API_Model: 'Local',
        API_Name: 'Get_Range_Money',
        Data: {
            local_type: '2', //地址类型 1字符串地址 2坐标地址
            address1: startPoint,
            address2: endPoint,
            car_id: carId,
            gotime: goTime
        }
    }
    const md5 = getMd5Sign(body)
    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5
            })
            .then((res: any) => {
                resolve(res.data.data)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

/**
 * 获取两地址间配送距离并计算金额（用于下单前调用）
 * @param startPoint
 * @param endPoint
 */
export const GetRangeMoney = (obj: any) => {
    const body = {
        API_Model: 'Local', //API模块名
        API_Name: 'Get_Range_Money', //API方法名
        Data: {
            local_type: '2', //地址类型 1字符串地址 2坐标地址
            address1: obj.address1, //起始地址 字符串类型 2
            address2: obj.address2, //终止地址 字符串类型 2
            car_id: obj.car_id, //车型ID
            gotime: obj.gotime
        }
    }
    const md5 = getMd5Sign(body)
    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5
            })
            .then((res: any) => {
                resolve(res.data.data)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

/**
 * 获取距离
 * @param startPoint
 * @param endPoint
 */
export const getDistant = (startPoint: string, endPoint: string) => {
    const body = {
        API_Model: 'Local', //API模块名
        API_Name: 'getLocalRange', //API方法名
        Data: {
            local_type: '2', //地理位置类型 1经纬度 2物理地址
            address1: startPoint, //此参数为起点地址，local_type为2时填写
            address2: endPoint //此参数为终点地址，local_type为2时填写
        } //规划路线信息返回
    }
    const md5 = getMd5Sign(body)
    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5
            })
            .then((res: any) => {
                resolve(res.data.data)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}
